using Com.SharpZebra.Commands;
using Com.SharpZebra.Commands.Codes;

namespace Com.SharpZebra.ExampleWinformApplication.Labels
{
    class LabelBuilder
    {
        private delegate IZebraCommand Command(string value, int x, int y);
        private string value;
        private Command commandToExecute;

        private ZebraCommands commands = new ZebraCommands();

        public LabelBuilder Barcode(string value)
        {
            return StoreValues(BarcodeCommand, value);
        }

        private LabelBuilder StoreValues(Command delegateCommand, string value)
        {
            this.value = value;
            commandToExecute = delegateCommand;
            return this;
        }

        public LabelBuilder At(int x, int y)
        {
            commands.Add(commandToExecute(value, x, y));
            commandToExecute = null;
            value = null;
            return this;
        }

        public LabelBuilder Text(string value)
        {
            return StoreValues(StandardText, value);
        }

        public LabelBuilder Image(string value)
        {
            return StoreValues(ImageCommand, value);
        }

        private IZebraCommand ImageCommand(string imageName, int x, int y)
        {
            return new GraphicZebraCommand(imageName, x, y);
        }

        private IZebraCommand BarcodeCommand(string barcode, int x, int y)
        {
            return ZebraCommands.BarCodeCommand(x, y, ElementRotation.NO_ROTATION, 1, 2, 4, 20, true, barcode);
        }

        private IZebraCommand StandardText(string text, int x, int y)
        {
            return ZebraCommands.TextCommand(x, y, ElementRotation.NO_ROTATION, StandardZebraFont.NORMAL, 1, 1, false, text);
        }


        public string ToZebraInstruction()
        {
            return commands.ToZebraInstruction();
        }

        public LabelBuilder Border(int startX, int startY, int endX, int endY)
        {
            commands.Add(ZebraCommands.DrawBox(startX, startY, 2, endX, endY));
            return this;
        }
    }
}
